﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

namespace Acoris.Content
{
    /// <summary>
    /// 表示文件夹路径集合
    /// </summary>
    public sealed class NotebookFolderCollection:INotifyCollectionChanged
    {
        private List<string> m_notebookFolderCollection;
        private HashSet<string> m_folderHashSet;
        private object m_lock;

        public event NotifyCollectionChangedEventHandler CollectionChanged;

        /// <summary>
        /// 初始化
        /// </summary>
        public NotebookFolderCollection()
        {
            m_notebookFolderCollection = new List<string>();
            m_folderHashSet = new HashSet<string>();
            m_lock = new object();
        }

        /// <summary>
        /// 将可能存在记事本的文件路径添加到当前集合之中
        /// </summary>
        /// <param name="folder">可能存在的记事本路径</param>
        public void Add(string folder)
        {
            lock (m_lock) {
                if (m_folderHashSet.Add(folder)) {
                    m_notebookFolderCollection.Add(folder);
                    CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, folder));
                }
            }
        }

        /// <summary>
        /// 移除指定路径
        /// </summary>
        /// <param name="folder">要移除的路径</param>
        public void Remove(string folder)
        {
            lock (m_lock) {
                if (m_folderHashSet.Contains(folder)) {
                    m_folderHashSet.Remove(folder);
                    m_notebookFolderCollection.Remove(folder);
                    CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, folder));
                }
            }
        }

        /// <summary>
        /// 清除当前集合
        /// </summary>
        public void Clear()
        {
            lock (m_lock) {
                m_notebookFolderCollection.Clear();
                m_folderHashSet.Clear();
            }
        }

        /// <summary>
        /// 获取一个值，该值指示指定的文件夹是否存在当前集合
        /// </summary>
        /// <param name="folder">指定要比较的对象</param>
        /// <returns>返回一个值，该值指示指定的文件夹是否存在当前集合</returns>
        public bool Contains(string folder)
        {
            return m_folderHashSet.Contains(folder);
        }

        /// <summary>
        /// 获取当前集合中元素的个数
        /// </summary>
        public int Count {
            get { return m_notebookFolderCollection.Count; }
        }

        /// <summary>
        /// 获取指定索引位置的文件夹路径
        /// </summary>
        /// <param name="index">指定的索引号</param>
        /// <returns>返回指定索引位置的文件夹路径</returns>
        public object this[int index] {
            get {
                var element = m_notebookFolderCollection[index];
                return element;
            }
        }
    }
}
